package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.TravelCondition;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.repository.TravelRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 用户服务接口实现
 */
@Service
//@Transactional  现在写不能事务, 需要讲完复制集才能使用事务
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelRepository repsitory;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IDestinationService destinationService;


    @Override
    public Travel save(Travel travel) {
        travel.setId(null);
        Travel travel1 = repsitory.save(travel);
        return travel1;
    }

    @Override
    public Travel update(Travel travel) {
        Travel travelDb = this.get(travel.getId());
        travel.setCreateTime(travelDb.getCreateTime());
        return repsitory.save(travel);
    }

    @Override
    public void delete(String id) {
        repsitory.deleteById(id);
    }

    @Override
    public Travel get(String id) {
        if (StringUtils.hasLength(id)){
            Travel travel = repsitory.findById(id).orElse(null);
            UserInfo userInfo = userInfoService.get(travel.getUserId());
            Destination destination = destinationService.get(travel.getDestId());
            travel.setDest(destination);
            travel.setAuthor(userInfo);
            return travel;
        }
        return null;
    }

    @Override
    public List<Travel> list() {
        return repsitory.findAll();
    }

    @Override
    public Page<Travel> query(TravelQuery qo) {
        Query query = new Query();
        if (qo.getDestId() != null && !"".equals(qo.getDestId())) {
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        if (qo.getDayType() != -1) {
            TravelCondition day = TravelCondition.TRAVEL_DAYS.get(qo.getDayType());
            query.addCriteria(Criteria.where("day").gte(day.getMin()).lte(day.getMax()));
        }

        if (qo.getPerExpendType() != -1) {
            TravelCondition per = TravelCondition.TRAVEL_PRE_EXPENDS.get(qo.getPerExpendType());
            query.addCriteria(Criteria.where("perExpend").gte(per.getMin()).lte(per.getMax()));
        }

        Pageable pageable;

        if (qo.getOrderType() == 1) {
            pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "releaseTime");
        } else if (qo.getOrderType() == 2) {
            pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "thumbsupnum");
        } else {
            pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        }


        Page<Travel> travels = DBHelper.query(template, query, qo, Travel.class, pageable);
        for (Travel travel : travels) {
            UserInfo user = userInfoService.get(travel.getUserId());
            travel.setAuthor(user);
        }
        return travels;
    }

    @Override
    public Travel saveOrUpdate(Travel travel) {

        travel.setLastUpdateTime(new Date());

        if (Travel.STATE_RELEASE == travel.getState()) {
            travel.setReleaseTime(new Date());
            travel.setIsPublic(Travel.ISPUBLIC_YES);
        } else {
            travel.setReleaseTime(null);
            travel.setIsPublic(Travel.ISPUBLIC_NO);
        }
        travel.setDestName(destinationService.get(travel.getDestId()).getName());

        if (StringUtils.hasLength(travel.getId())) {
            //update
            return this.update(travel);
        } else {
            //save
            travel.setCreateTime(new Date());
            return this.save(travel);
        }
    }

    @Override
    public void audit(String id, int state) {
        Travel travel = this.get(id);
        travel.setState(state);
        travel.setLastUpdateTime(new Date());
        this.saveOrUpdate(travel);
    }

    @Override
    public Travel getContentById(String id) {
        return this.get(id);
    }

    @Override
    public List<Travel> findByViewnum(String destId) {
        Pageable pageable = PageRequest.of(0, 3, Sort.Direction.DESC, "viewnum");
        List<Travel> list = repsitory.findByDestId(destId, pageable);
        return list;
    }

    @Override
    public List<Travel> getByDestName(String keyword) {
        List<Travel> trs = repsitory.findByDestName(keyword);
        for (Travel tr : trs) {
            tr.setAuthor(userInfoService.get(tr.getUserId()));
            tr.setDest(destinationService.get(tr.getDestId()));
        }
        return trs;
    }
}
